/*
 * utility methods used in the code
 */
var Util = new Class({
  findMaximumDivider: function(number,divider){
      var ooo=parseInt(number/divider);
      return ooo*divider;
  },
  rePositionCoordinate: function(number,divider){
      var modulo=number%divider;
      var maxDivider=parseInt(number/divider);
      if(modulo==0)
          return number;
  	if(modulo>(4/10)*divider && modulo<(6/10)*divider){
            if(maxDivider%2==0){
                return maxDivider*divider;
            } else
  		return number;
  	} else {
  		if(modulo<=(4/10)*divider){
  			return maxDivider*divider;
  		}
  		if(modulo>=(6/10)*divider){
  			return (maxDivider+1)*divider;
  		}
  	}
  },
  getZoneAtPoint: function(x,y){
      if(x>=game.worldWidth)
          x=game.worldWidth-1;
      if(y>=game.worldHeight)
          y=game.worldHeight-1;
      return game.zones[parseInt(x/game.standardWidth)][parseInt(y/game.standardHeight)];
  },
  removeElementFromArray: function(el,array){
      var idx=-1;
        for(var i=0;i<array.length;i++){
            if(array[i]==el){
                idx=i;
                break;
            }
        }
        if(idx!=-1)
          array.splice(i,1);
  }
});

// gets the map string for the server
function getMap(){
  var lines = new Array(game.zones[0].length);
  for(var i=0;i<game.zones.length;i++){
      for(var k=0;k<game.zones[i].length;k++){
          if(i==0)
              lines[k]='';
          var zone = game.zones[i][k];
          if(zone.freeZone)
            lines[k]+=' ';
          else
            lines[k]+=zone.getElements()[0].symbol;
      }
  }
  return lines.join('N');
}

function getJSPlayersStatus(){
    var status='players:';
    if($defined(game.hero))
        status+=game.hero.username+'^'+game.hero.x+'^'+game.hero.y;
    for(var i=0;i<game.heroes.length;i++){
        status+='~'+game.heroes[i].username+'^'+game.heroes[i].x+'^'+game.heroes[i].y;
    }
    return status;
}
var DynaSound = new Class({
  initialize: function(url){
      this.flashWorking=false;
      this.mySwiff = new Swiff(url,{width: 1, height: 1, container: $('swfContainer'), swLiveConnect: true});
  },
  initSounds: function(){
      this.sound = this.mySwiff.toElement();
      if((!Browser.Engine.trident && this.sound.data.indexOf('DynaSound.swf')!=-1) || (Browser.Engine.trident && this.sound.innerHTML.indexOf('DynaSound.swf')!=-1))
          this.flashWorking=true;
      Swiff.remote(this.sound,'loadSound','bgsound','sounds/bgsound.mp3');
      Swiff.remote(this.sound,'loadSound','bomb4','sounds/bomb4.mp3');
      Swiff.remote(this.sound,'loadSound','bomb5','sounds/bomb5.mp3');
      Swiff.remote(this.sound,'loadSound','bomb6','sounds/bomb6.mp3');
      Swiff.remote(this.sound,'loadSound','scream1','sounds/scream1.mp3');
      Swiff.remote(this.sound,'loadSound','scream2','sounds/scream2.mp3');
      Swiff.remote(this.sound,'loadSound','scream3','sounds/scream3.mp3');
      Swiff.remote(this.sound,'loadSound','scream4','sounds/scream4.mp3');
  },
  playBombExplosionSound: function(){
    if(this.flashWorking)
      Swiff.remote(this.sound,'playSound','bomb4');
  },
  playBGSound: function(){
      if(this.flashWorking)
        Swiff.remote(this.sound,'playSoundManyTimes','bgsound',999);
  },
  playSound: function(soundName){
      if(this.flashWorking)
        Swiff.remote(this.sound,'playSound',soundName);
  },
  stopBGSound: function(){
      if(this.flashWorking)
        Swiff.remote(this.sound,'stopSound','bgsound');
  },
  stopBombSound: function(){
      if(this.flashWorking)
        Swiff.remote(this.sound,'stopSound','bomb');
  },
  setVolume: function(volume){ // volume between 0 and 100
      if(volume<0 || volume>100)
          return;
      if(this.flashWorking){
        Swiff.remote(this.sound,'setVolume','bomb',volume/100);
        Swiff.remote(this.sound,'setVolume','bgsound',volume/100);
      }
  }
});